package com.hui.xiao.xiaohui.io;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.hui.xiao.xiaohui.R;

import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

/**
 * Created by Administrator on 2016/6/16.
 */
public class HttpTest extends Activity {
    TextView show;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0x123) {
                String html = msg.getData().getString("html");
                show.setText(html);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.http);
        show = (TextView) findViewById(R.id.show);
        Button down = (Button) findViewById(R.id.down);
        down.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread() {
                    @Override
                    public void run() {
                        InputStream inputStream = null;
                        try {
                            URL url = new URL("http://cn.bing.com/");
                            URLConnection conn = url.openConnection();
                            HttpURLConnection httpConn = (HttpURLConnection) conn;
                            httpConn.setRequestMethod("POST");
                            httpConn.setConnectTimeout(5000);
                            httpConn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
                            httpConn.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");
                            httpConn.setRequestProperty("Connection", "keep-alive");
                            httpConn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36");
                            inputStream = httpConn.getInputStream();
                            String html = IOUtils.toString(inputStream);
                            Bundle bundle = new Bundle();
                            bundle.putString("html", html);
                            Message msg = new Message();
                            msg.what = 0x123;
                            msg.setData(bundle);
                            handler.sendMessage(msg);
                        } catch (IOException e) {
                            e.printStackTrace();
                        } finally {
                            IOUtils.closeQuietly(inputStream);
                        }
                    }
                }.start();
            }
        });
    }
}
